package net.zwet.clanworks.connectors.eve.api.connector;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import net.zwet.clanworks.connectors.eve.api.exceptions.EveConnectionException;
import net.zwet.clanworks.connectors.eve.api.connector.url.IEveApiUrl;
import net.zwet.clanworks.connectors.eve.api.connector.url.Parameters;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


abstract class ConnectorBase {
    /**
     * Log variable for all child classes. Uses LogFactory.getLog(getClass()) from Commons Logging
     */
    protected final Log log = LogFactory.getLog(getClass());
    
	protected String userID;
	protected String apiKey;
	
	// --------------------------------------------------------------------------------------[ Constructors ]-----

    public ConnectorBase ( String userID, String apiKey ) {
        this.userID = userID;
        this.apiKey = apiKey;
    }

    public ConnectorBase () {
		this.userID = "";
		this.apiKey = "";
	}

    protected void InitializeEnvironment() {
    }

	// --------------------------------------------------------------------------------------[ Methods ]-----

	protected InputStream connect(IEveApiUrl url) throws EveConnectionException {
		return connect(url, null);
	}

	/**
	 * The subclass that wants to retrieve information must call this method.
	 * Based on the provided url and parameters it will create a connection to
	 * the EveService API and retrieve the xml
	 * 
	 * @param url
	 * @param parameters
	 * @return
	 */
	protected InputStream connect(IEveApiUrl url, Parameters parameters) throws EveConnectionException {
		InputStream inStream = null;
		
		try {
			HttpClient client = new DefaultHttpClient();
			HttpPost httpPost = new HttpPost(url.getUrl());

			if (parameters != null) {
				UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(
						parameters.getParameters(), "ISO-8859-1");

				httpPost.setEntity(p_entity);
			}

			/* Perform the actual HTTP POST */
			HttpResponse response = client.execute(httpPost);
			HttpEntity entity = response.getEntity();
			inStream = entity.getContent();
		} catch (ClientProtocolException cpeExc) {
			log.debug("Client Protocol Exception: " + cpeExc.getMessage());
		} catch (IllegalStateException ise) {
			log.debug("Connect exception: " + ise.getMessage());
		} catch (IOException ioe) {
			log.debug("IO Exception: " + ioe.getMessage());
		}
		
		return inStream;
	}
	
	protected InputStream connect ( String uri ) throws EveConnectionException {
		InputStream inStream = null;
		
		try {
			URL url = new URL(uri); //you can write here any link
	
			/* Open a connection to that URL. */
			URLConnection ucon = url.openConnection();
	
			/*
			 * Define InputStreams to read from the URLConnection.
			 */
			inStream = ucon.getInputStream();
		} catch (IOException e) {
			// TODO errorhandling
			// Error
		}
		
		return inStream; 
	}
	
	// --------------------------------------------------------------------------------------[ Getters & Setters ]-----

	public String getUserID() {
		return userID;
	}

	public void setUserID(String userID) {
		this.userID = userID;
	}

	public String getApiKey() {
		return apiKey;
	}

	public void setApiKey(String apiKey) {
		this.apiKey = apiKey;
	}
	
}

